import {
	Toast
} from 'vant'
import {
	getuserInfo
} from '@/api/user';
let {
	token,
	tokenExpired
} = localStorage;

import store from '@/store'


function init() {


	return new Promise((resolve, reject) => {

		if (token) {

			let now = +new Date();
			if (tokenExpired <= now) {
				Toast('检测到您长时间未登录,请重新登陆');
				localStorage.clear();
				resolve()
			} else {

				let toast1 = Toast.loading({
					message: '初始化...',
					forbidClick: true,
				});

				getuserInfo({
					token
				}).then(res => {
					console.log(res)

					setTimeout(v => {
						toast1.clear();
					}, 300)
					resolve();
					if(res.code == 0){
						//  去把vuex state里的数据改一下
						store.commit('user/SET_lOGIN_STATE',true)
						store.commit('user/setUserInfo',res.userInfo)
						store.commit('user/setUid',res.uid)
					}
				


				})

			}

		} else {
			resolve()
		}


	})


}


export default init;
